package com.moss.system.service.impl.sample;

import com.moss.common.constant.Constants;
import com.moss.common.entity.SysSampleEntity;
import com.moss.common.entity.SysSampleItemEntity;
import com.moss.common.entity.SysSampleTaskEntity;
import com.moss.common.util.IdUtil;
import com.moss.system.mapper.SampleItemMapper;
import com.moss.system.mapper.SampleMapper;
import com.moss.system.mapper.SampleTaskMapper;
import com.moss.system.service.SampleBranchService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SampleTaskServiceImpl implements SampleBranchService {

    @Resource
    SampleMapper sampleMapper;

    @Resource
    SampleItemMapper sampleItemMapper;

    @Resource
    SampleTaskMapper sampleTaskMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSample(SysSampleEntity sample) {
        sampleMapper.insert(sample);
        List<SysSampleItemEntity> itemList = sample.getItemList();
        itemList.forEach(o -> o.setSampleId(sample.getSampleId()));
        sampleItemMapper.insert(itemList);

        SysSampleTaskEntity task = sample.getTask();
        task.setSampleId(sample.getSampleId());
        sampleTaskMapper.insert(task);
    }

    @Override
    public String type() {
        return Constants.TASK_SAMPLE;
    }

    @Override
    public SysSampleEntity getDetail(String sampleId) {
        SysSampleEntity sample = sampleMapper.selectById(sampleId);
        SysSampleTaskEntity sampleTask = sampleTaskMapper.selectById(sampleId);
        sample.setTask(sampleTask);
        return sample;
    }
}
